/*
 * TowerBlueOne.java
 *
 * Created on 27 December 2007, 12:37
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.tower;

import hextd.hex.Hex;
import hextd.creep.Creep;
import hextd.core.Context;
import hextd.util.Geometry;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Area;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;

/**
 *
 * @author hylke
 */
public class TowerBlueOne extends TowerBase {

	public static int price = 500;
	public static int damage = 0;
	public static float range = 5f;
	//
	private Creep.COLOR damageColor = Creep.COLOR.BLUE;
	private int coolDown = 0;
	private int coolDownMax = 79;
	private String[] newModes = {"Fastest"};
	//
	private Color transColor;
	//
	private Creep[] currentTargets = new Creep[0];

	/** Creates a new instance of TowerBlueOne */
	public TowerBlueOne(Context context, Hex hex, int owner, int activateTick) {
		super(TowerFactory.type.blueOne, price, damage, range, owner, activateTick);
		this.lineColor = new Color(75, 75, 255);
		hexInfluence = 1;
		this.transColor = new Color(this.lineColor.getRed(), this.lineColor.getGreen(), this.lineColor.getBlue(), 75);
		this.modes = this.newModes;
		this.doInit(context, hex);

	}

	@Override
	protected void makeButtons() {
		super.makeButtons();
		pts.setCanLock(false);
	}

	@Override
	public String getStatusString() {
		String retString = "";
		retString += "<h1>Blue Tower 1</h1>" +
				"<p>" +
				"<b>Level:</b> " + (1 + this.level) + "<br>\n" +
				"<b>Slowdown:</b> " + TowerBlueOne.form.format(100f - 300f / (4 + this.level)) + "%<br>\n" +
				"<b>Range:</b> " + this.rangeText + "<br>" +
				"</p>\n";
		return retString;
	}

	@Override
	public String getInfoString() {
		String retString = "";
		retString += "<h1>Blue Tower 1</h1>" +
				"<p>" +
				"Tower with four energy beams that overheat the targets engines, reducing it's top speed.<br>" +
				"More hits make the effect last longer and upgrading the tower makes the effect stronger." +
				"<br>It's effect is strong against blue enemies but very weak against yellow enemies." +
				"</p>" +
				"<p>" +
				"<b>Price:</b> " + TowerBlueOne.price + "<br>" +
				"<b>Damage:</b> none, slowdown instead<br>" +
				"<b>Range:</b> " + TowerBlueOne.form.format(rangeCurrent) + "<br>" +
				"<b>Refire rate:</b> 0.25/s" +
				"</p>";
		return retString;
	}

	@Override
	public void runTick(int gameTime) {
		if (this.coolDown > 0) {
			this.coolDown--;
		} else {
			this.currentTargets = this.findEnemiesFastest(this.xFloat, this.yFloat, this.rangeCurrent, 4, false);
			if (this.currentTargets.length > 0) {
				Creep enemy;
				for (int i = 0; i < this.currentTargets.length; i++) {
					enemy = this.currentTargets[i];
					enemy.doDamage(this.getOwner(), 3f / (4f + this.level), Creep.DamageType.power, this.damageColor);
				}

				this.coolDown = this.coolDownMax;
			}
		}
	}

	@Override
	protected void generateShapes() {

		int shapeCount = 3;
		int i;
		float[][] points = Geometry.createHexCoords(this.paintScale);
		float[][] points2 = Geometry.createHexCoords(this.paintScale * 0.2f);

		this.shapes = new Shape[shapeCount];
		i = 0;
		GeneralPath poly = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
		poly.append(new CubicCurve2D.Float(points[0][0], points[0][1], points2[0][0], points2[0][1], points2[2][0], points2[2][1], points[2][0], points[2][1]), true);
		poly.append(new CubicCurve2D.Float(points[2][0], points[2][1], points2[2][0], points2[2][1], points2[4][0], points2[4][1], points[4][0], points[4][1]), true);
		poly.append(new CubicCurve2D.Float(points[4][0], points[4][1], points2[4][0], points2[4][1], points2[0][0], points2[0][1], points[0][0], points[0][1]), true);
		poly.append(new CubicCurve2D.Float(points[1][0], points[1][1], points2[1][0], points2[1][1], points2[3][0], points2[3][1], points[3][0], points[3][1]), false);
		poly.append(new CubicCurve2D.Float(points[3][0], points[3][1], points2[3][0], points2[3][1], points2[5][0], points2[5][1], points[5][0], points[5][1]), true);
		poly.append(new CubicCurve2D.Float(points[5][0], points[5][1], points2[5][0], points2[5][1], points2[1][0], points2[1][1], points[1][0], points[1][1]), true);
		poly.append(new Ellipse2D.Float(-0.6f * this.paintScale, -0.6f * this.paintScale, 1.2f * this.paintScale, 1.2f * this.paintScale), false);
		this.shapes[i++] = new Area(poly);
		this.shapes[i++] = this.shapes[0];
		this.shapes[i++] = new Ellipse2D.Float(-1.0f * strokeScale, -1.0f * strokeScale, 2.0f * strokeScale, 2.0f * strokeScale);

		i = 0;
		this.shapeAction = new int[shapeCount];
		this.shapeAction[i++] = 2;
		this.shapeAction[i++] = 1;
		this.shapeAction[i++] = 1;

		i = 0;
		this.shapeColor = new Color[shapeCount];
		this.shapeColor[i++] = this.transColor;
		this.shapeColor[i++] = this.lineColor;
		this.shapeColor[i++] = this.lineColor;

		i = 0;
		this.strokes = new Stroke[shapeCount];
		this.strokes[i++] = new BasicStroke(2.0f * strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
		this.strokes[i++] = this.strokes[0];
		this.strokes[i++] = this.strokes[0];
	}

	@Override
	public void paintEffects(Graphics2D g2, int gameTime) {
		Stroke defaultStroke = g2.getStroke();

		if (this.currentTargets.length > 0 && this.coolDown > this.coolDownMax - 3) {
			g2.setColor(this.lineColor);
			for (int i = 0; i < this.currentTargets.length; i++) {
				g2.draw(new Line2D.Float(this.xPaint, this.yPaint, currentTargets[i].getPaintX(), currentTargets[i].getPaintY()));
			}
		}

		g2.setStroke(defaultStroke);
	}
}
